import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/event_bus_util.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:material_collection_app/widget/common/round_checkbox.dart';

class EnterCheckItem extends StatefulWidget {
  final String baseApi;
  final Map item;
  final int itemIndex;
  final List selected;
  final handleDefaultAction;
  final getStoreInfo;
  final showConfirmInfoDialog;
  final showAddMaterialDialog;

  EnterCheckItem(
      {Key key,
      this.baseApi = 'enter/',
      this.item,
      this.itemIndex,
      this.selected,
      this.getStoreInfo,
      this.handleDefaultAction,
      this.showConfirmInfoDialog,
      this.showAddMaterialDialog})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<EnterCheckItem> {
  var eventBus = new EventBus();

  // 删除物资
  deleteMaterial(Map item) {
    DioUtils.request("${widget.baseApi}delMaterial",
        context: context,
        method: "POST",
        params: {'id': item['id']}, onSuccess: (res) {
      if (res['code'] == 1) {
        eventBus.emit('enter_delete', {'index': widget.itemIndex});
      }
      Utils.showText(text: res['msg']);
    }, onError: (error) {});
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    var typeNames = 'mtype';

    //区分来自于报废入库和设备更换入库
    if (widget.baseApi == 'scrap_enter/' ||
        widget.baseApi == 'allocate_enter/') {
      typeNames = 'mtypes';
    }

    var item = widget.item;
    String typeName =
        item[typeNames] != null ? item[typeNames]['type_name'] : '';
    String storeName = item['store'] != null ? item['store']['store_name'] : '';
    String libraryName =
        item['library'] != null ? item['library']['library_name'] : '';
    String shelvesName =
        item['shelves'] != null ? item['shelves']['detail_code'] : '';

    int sId = item['store'] != null ? item['store']['store_id'] : null;
    int lId =
        item['library'] != null ? item['library']['store_library_id'] : null;
    int shId =
        item['shelves'] != null ? item['shelves']['store_detail_id'] : null;
    String itemIndex = (widget.itemIndex + 1).toString();

    Widget _buildSon() {
      if (widget.baseApi == 'enter/') {
        return SizedBox(
          width: 160.w,
          height: 66.w,
          child: ElevatedButton(
            style: ElevatedButton.styleFrom(
                padding: EdgeInsets.symmetric(horizontal: 0),
                backgroundColor: Colors.transparent,
                // backgroundColor: Utils.hexToColor('#00ab9d'),
                shape: RoundedRectangleBorder(
                    // side: BorderSide(color: Colors.red, width: 1),
                    borderRadius: BorderRadius.all(Radius.circular(12.w)))),
            onPressed: () {
              Navigator.pushNamed(context, '/son_material',
                  arguments: {'item': widget.item});
            },
            child: Text('子物资',
                style: TextStyle(
                    color: Utils.hexToColor('#FFFFFF'), fontSize: 14)),
          ),
        );
      } else {
        return Container();
      }
    }

    return InkWell(
      onTap: () {},
      child: Container(
          width: 700.w,
          height: 430.w,
          margin: EdgeInsets.fromLTRB(25.w, 10, 25.w, 0),
          padding: EdgeInsets.fromLTRB(24.w, 0, 24.w, 0.w),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20.w),
            border: Border.all(
                width: 1,
                color: item['errCode'] == 1 ? Colors.red : Colors.white),
          ),
          child: new Dismissible(
            key: Key('a$sId'),
            secondaryBackground: Container(
              child: Padding(
                padding: EdgeInsets.only(right: 19),
                child: Text('删除',
                    style: TextStyle(fontSize: 20, color: Colors.white)),
              ),
              color: Colors.red,
              alignment: Alignment.centerRight,
            ),
            background: Container(
              color: Colors.blue,
            ),
            direction: DismissDirection.endToStart,
            confirmDismiss: (DismissDirection direction) async {
              if (direction == DismissDirection.endToStart) {
                Utils.openAlert('确定要删除吗？', context).then((confirm) {
                  if (confirm) {
                    deleteMaterial(item);
                    return true;
                  } else {
                    return false;
                  }
                });
              }
            },
            onDismissed: (direction) {
              // materialList.removeAt(index);
              // Scaffold.of(context).showSnackBar(
              //   new SnackBar(content: new Text("删除成功"))
              // );
            },
            child: Row(
              children: <Widget>[
                Stack(
                  children: <Widget>[
                    Container(
                        height: 45,
                        // margin: EdgeInsets.only(top: 14, left: 5),
                        child: Center(
                          child: Text(item['sort'].toString()),
                          // child: Text((widget.itemIndex + 1).toString()),
                        )),
                    RoundCheckBox(
                      select: widget.selected[widget.itemIndex]['selected'],
                      onChanged: () {
                        setState(() {
                          widget.selected[widget.itemIndex]['selected'] =
                              !widget.selected[widget.itemIndex]['selected'];
                        });
                      },
                    ),
                  ],
                ),
                Container(
                  width: 580.w,
                  margin: EdgeInsets.only(left: 20.w),
                  child: Column(
                    children: <Widget>[
                      Container(
                        height: 45,
                        decoration: BoxDecoration(
                            border: Border(
                                bottom: BorderSide(
                                    color: Utils.hexToColor('#EEEEEE')))),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Container(
                              width: 580.w,
                              child: Row(
                                children: <Widget>[
                                  Text(
                                    'RFID号：',
                                    style: TextStyle(fontSize: 13),
                                  ),
                                  Expanded(
                                    child: Text(
                                      item['rfid'],
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                      style: TextStyle(fontSize: 12),
                                    ),
                                  )
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      Expanded(
                          child: Container(
                              alignment: Alignment.topLeft,
                              padding: EdgeInsets.fromLTRB(0, 0.w, 0, 34.w),
                              child: Column(
                                children: <Widget>[
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Stack(
                                        children: <Widget>[
                                          Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start, // 左对齐
                                            children: <Widget>[
                                              Container(
                                                margin:
                                                    EdgeInsets.only(top: 34.w),
                                                child: Text(
                                                    item['has_img'] == true
                                                        ? item['name']
                                                        : item['name'] +
                                                            "(未拍照)",
                                                    // overflow: TextOverflow.ellipsis,
                                                    // maxLines: 3,
                                                    style: TextStyle(
                                                        color:
                                                            item['has_img'] ==
                                                                    true
                                                                ? Color(
                                                                    0xFF333333)
                                                                : Colors.red,
                                                        fontSize: 15,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                        height: 1)),
                                              ),
                                              Container(
                                                margin:
                                                    EdgeInsets.only(top: 23.w),
                                                child: Text(
                                                    '子物资数量：' +
                                                        (item['son'] != null
                                                            ? item['son']
                                                                .toString()
                                                            : ''),
                                                    style: TextStyle(
                                                        // color: item['son'] > 0
                                                        //     ? Color(0xFF666666)
                                                        //     : Colors.red,

                                                        color:
                                                            Color(0xFF666666),
                                                        // color: item['son'] !=
                                                        //         null
                                                        //     ? Color(0xFF666666)
                                                        //     : Colors.red,
                                                        fontSize: 12,
                                                        height: 1)),
                                              ),
                                              Container(
                                                margin:
                                                    EdgeInsets.only(top: 23.w),
                                                child: Text('物资分类：' + typeName,
                                                    style: TextStyle(
                                                        color:
                                                            Color(0xFF666666),
                                                        fontSize: 12,
                                                        height: 1)),
                                              ),
                                              Row(
                                                children: <Widget>[
                                                  Container(
                                                    width: 250.w,
                                                    margin: EdgeInsets.only(
                                                        top: 13.w),
                                                    child: Text(
                                                        '物资数量：' +
                                                            item['num']
                                                                .toString(),
                                                        maxLines: 1,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                        style: TextStyle(
                                                            color: Color(
                                                                0xFF666666),
                                                            fontSize: 12,
                                                            height: 1)),
                                                  ),
                                                  Container(
                                                    width: 300.w,
                                                    margin: EdgeInsets.only(
                                                        left: 30.w, top: 13.w),
                                                    child: Text(
                                                        '所属仓库：' + storeName,
                                                        maxLines: 1,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                        style: TextStyle(
                                                            color: Color(
                                                                0xFF666666),
                                                            fontSize: 12,
                                                            height: 1)),
                                                  ),
                                                ],
                                              ),
                                              Row(
                                                children: <Widget>[
                                                  Container(
                                                    width: 250.w,
                                                    margin: EdgeInsets.only(
                                                        top: 13.w),
                                                    child: Text(
                                                        '推荐库区：' + libraryName,
                                                        maxLines: 1,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                        style: TextStyle(
                                                            color: Color(
                                                                0xFF666666),
                                                            fontSize: 12,
                                                            height: 1)),
                                                  ),
                                                  Container(
                                                    width: 300.w,
                                                    margin: EdgeInsets.only(
                                                        left: 30.w, top: 13.w),
                                                    child: Text(
                                                        '所属库位：' + shelvesName,
                                                        maxLines: 1,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                        style: TextStyle(
                                                            color: Color(
                                                                0xFF666666),
                                                            fontSize: 12,
                                                            height: 1)),
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ],
                                  ),
                                  Expanded(
                                      child: Container(
                                    margin: EdgeInsets.only(top: 10),
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.end,
                                      children: <Widget>[
                                        Container(
                                          margin: EdgeInsets.only(right: 10),
                                          decoration: BoxDecoration(
                                              color:
                                                  Utils.hexToColor('#33CCFF'),
                                              borderRadius:
                                                  new BorderRadius.circular(
                                                      12.w)),
                                          child: _buildSon(),
                                        ),
                                        Container(
                                          margin: EdgeInsets.only(right: 10),
                                          child: SizedBox(
                                            width: 160.w,
                                            height: 66.w,
                                            child: ElevatedButton(
                                              style: ElevatedButton.styleFrom(
                                                  padding: EdgeInsets.symmetric(
                                                      horizontal: 0),
                                                  // color: Utils.hexToColor('#00ab9d'),
                                                  shape: RoundedRectangleBorder(
                                                      side: BorderSide(
                                                          color: Colors.red,
                                                          width: 1),
                                                      borderRadius:
                                                          BorderRadius.all(
                                                              Radius.circular(
                                                                  8.w)))),
                                              onPressed: () {
                                                widget.handleDefaultAction(
                                                    sId,
                                                    lId,
                                                    shId,
                                                    item['id'],
                                                    widget.itemIndex);
                                                widget.getStoreInfo(
                                                    sId, lId, shId, context);
                                              },
                                              child: Text('选择库位',
                                                  style: TextStyle(
                                                      color: Utils.hexToColor(
                                                          '#FF9232'),
                                                      fontSize: 14)),
                                            ),
                                          ),
                                        ),
                                        Offstage(
                                          offstage: item['confirm'] == 1
                                              ? true
                                              : false,
                                          child: SizedBox(
                                            width: 160.w,
                                            height: 66.w,
                                            child: ElevatedButton(
                                              style: ElevatedButton.styleFrom(
                                                  padding: EdgeInsets.symmetric(
                                                      horizontal: 0),
                                                  backgroundColor:
                                                      Utils.hexToColor(
                                                          '#00ab9d'),
                                                  shape: RoundedRectangleBorder(
                                                      borderRadius:
                                                          BorderRadius.all(
                                                              Radius.circular(
                                                                  8.w)))),
                                              onPressed: () {
                                                widget.showConfirmInfoDialog(
                                                    item['id'],
                                                    item,
                                                    widget.itemIndex);
                                              },
                                              child: Text('确认信息',
                                                  style: TextStyle(
                                                      color: Colors.white,
                                                      fontSize: 14)),
                                            ),
                                          ),
                                        ),
                                        Offstage(
                                          offstage: item['confirm'] == 1
                                              ? false
                                              : true,
                                          child: SizedBox(
                                            width: 160.w,
                                            height: 66.w,
                                            child: ElevatedButton(
                                              style: ElevatedButton.styleFrom(
                                                  padding: EdgeInsets.symmetric(
                                                      horizontal: 0),
                                                  backgroundColor:
                                                      Utils.hexToColor(
                                                          '#297CFF'),
                                                  shape: RoundedRectangleBorder(
                                                      borderRadius:
                                                          BorderRadius.all(
                                                              Radius.circular(
                                                                  8.w)))),
                                              onPressed: () {
                                                // setState(() {
                                                //   updateStoresId = item['id'];
                                                // });
                                                widget.showAddMaterialDialog(
                                                    'edit',
                                                    itemInd: widget.itemIndex,
                                                    item: item);
                                              },
                                              child: Text('编辑物资',
                                                  style: TextStyle(
                                                      color: Colors.white,
                                                      fontSize: 14)),
                                            ),
                                          ),
                                        )
                                      ],
                                    ),
                                  ))
                                ],
                              )))
                    ],
                  ),
                )
              ],
            ),
          )),
    );
  }
}
